<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BCacheManager
 *
 * @author Administrator
 */
class BCacheManager {

    const Action_Reload = 'reload';
    const Event_AddNewSpace = 'event_add_new_space';
    const Event_Change_User_Role = 'event_change_user_role';

    protected $events = array();
    protected $cache = array();

    //put your code here
    public function getCache($id, $data = NULL, $scope = 'scope_user') {
        if ($id == 'userSpaces' && $this->getReloadList($id) == TRUE) {
            $this->resetReloadList($id);
            return FALSE;
        }
        if ($id == 'userRolesCache' && $this->getReloadList($id) == TRUE) {
            $this->resetReloadList($id);
            return FALSE;
        }


        if ($scope == 'scope_user') {
            return Blyn::app()->getCurrentUser()->getContextVariable($id);
        }
    }

    public function addCache($id, $value, $data = NULL, $scope = 'scope_user') {

        if ($scope == 'scope_user') {
            Blyn::app()->getCurrentUser()->addContextVariable($id, $value);
        }
    }

    public function addCacheEvent($eventName, $value, $scope = 'scope_user') {

        if ($scope == 'scope_user') {
            $this->events = $this->getCacheEvent();
            $this->events[$eventName] = $value;
            Blyn::app()->getCurrentUser()->addContextVariable('cache_events', $this->events);
        }

        if ($this->getCacheEvent(self::Event_AddNewSpace) != NULL && $this->getCacheEvent(self::Event_AddNewSpace) == TRUE) {
            $this->addReloadList('userSpaces', TRUE);
            $this->addReloadList('userRolesCache', TRUE);
        }

        if ($this->getCacheEvent(self::Event_Change_User_Role) != NULL && $this->getCacheEvent(self::Event_Change_User_Role) == TRUE) {
            $this->addReloadList('userRolesCache', TRUE);
        }
    }

    protected function getCacheEvent($eventName = NULL, $scop = 'scope_user') {
        if ($this->events == NULL)
            if ($scop = 'scope_user')
                $this->events = Blyn::app()->getCurrentUser()->getContextVariable('cache_events');

        if ($eventName != NULL) {
            if (isset($this->events[$eventName]))
                return $this->events[$eventName];
            else {
                return FALSE;
            }
        }

        return $this->events;
    }

    protected function getReloadList($id = null, $scop = 'scope_user') {
        if ($scop == 'scope_user')
            $reloadList = Blyn::app()->getCurrentUser()->getContextVariable('cache_reloadList');

        if ($id == NULL)
            return $reloadList;
        else
            return isset($reloadList[$id]) ? $reloadList[$id] : FALSE;
        
        return FALSE;
    }

    public function addReloadList($id, $value = true, $scope = 'scope_user') {

        if ($scope == 'scope_user')
            $reloadList = Blyn::app()->getCurrentUser()->getContextVariable('cache_reloadList');

        if ($reloadList == NULL)
            $reloadList = array();

        $reloadList[$id] = $value;

        Blyn::app()->getCurrentUser()->addContextVariable('cache_reloadList', $reloadList);
    }

    public function resetReloadList($id = NULL, $scope = 'scope_user') {
        if ($scope == 'scope_user') {
            if ($id == NULL) {
                $reloadList = array();
                Blyn::app()->getCurrentUser()->addContextVariable('cache_reloadList', $reloadList);
            } else
                $this->addReloadList($id);
        }
    }

}

?>
